using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Fieldbook.Entities;
using Fieldbook.Common;
namespace Fieldbook.DAL
{
	public static class MeasureunitDAO
	{
		#region Insert
		private static DbCommand insertCommand = null;
		private static string insertSQL = "INSERT INTO dbo.MeasureUnit (CATEGORYID, DESCRIPTION) VALUES (@CATEGORYID, @DESCRIPTION) SET @MEASUREUNITID = SCOPE_IDENTITY()";
		private static DbCommand GetInsertCommand(SqlDatabase db)
		{
			if (insertCommand == null)
			{
				insertCommand = db.GetSqlStringCommand(insertSQL);
				db.AddOutParameter(insertCommand, "@MEASUREUNITID", DbType.Int32, 4);
				db.AddInParameter(insertCommand, "@CATEGORYID", DbType.Int32);
				db.AddInParameter(insertCommand, "@DESCRIPTION", DbType.String);
			}
			return insertCommand;
		}
		private static void SetInsertValues(SqlDatabase db, DbCommand cmd, Measureunit entity)
		{
			db.SetParameterValue(cmd, "@CATEGORYID", entity.Categoryid);
			db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
		}
		public static bool InsertMeasureunit(Measureunit entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			if (res)
			{
				entity.Measureunitid = (int?)db.GetParameterValue(cmd, "@MEASUREUNITID");
			}
			return res;
		}
		public static bool InsertMeasureunit(Measureunit entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			if (res)
			{
				entity.Measureunitid = (int?)db.GetParameterValue(cmd, "@MEASUREUNITID");
			}
			return res;
		}
		public static bool InsertListMeasureunit(List<Measureunit> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetInsertCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Measureunit entity = list[i];
					SetInsertValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
					if (res)
					{
						entity.Measureunitid = (int?)db.GetParameterValue(cmd, "@MEASUREUNITID");
					}
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool InsertListMeasureunit(List<Measureunit> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetInsertCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Measureunit entity = list[i];
				SetInsertValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				if (res)
				{
					entity.Measureunitid = (int?)db.GetParameterValue(cmd, "@MEASUREUNITID");
				}
			}
			return res;
		}
		#endregion
		#region Update
		private static DbCommand updateCommand = null;
		private static string updateSQL = "UPDATE dbo.MeasureUnit SET CATEGORYID = @CATEGORYID, DESCRIPTION = @DESCRIPTION WHERE MEASUREUNITID = @MEASUREUNITID";
		private static DbCommand GetUpdateCommand(SqlDatabase db)
		{
			if (updateCommand == null)
			{
				updateCommand = db.GetSqlStringCommand(updateSQL);
				db.AddInParameter(updateCommand, "@MEASUREUNITID", DbType.Int32);
				db.AddInParameter(updateCommand, "@CATEGORYID", DbType.Int32);
				db.AddInParameter(updateCommand, "@DESCRIPTION", DbType.String);
			}
			return updateCommand;
		}
		private static void SetUpdateValues(SqlDatabase db, DbCommand cmd, Measureunit entity)
		{
			db.SetParameterValue(cmd, "@MEASUREUNITID", entity.Measureunitid);
			db.SetParameterValue(cmd, "@CATEGORYID", entity.Categoryid);
			db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
		}
		public static bool UpdateMeasureunit(Measureunit entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool UpdateMeasureunit(Measureunit entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			return res;
		}
		public static bool UpdateListMeasureunit(List<Measureunit> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetUpdateCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Measureunit entity = list[i];
					SetUpdateValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool UpdateListMeasureunit(List<Measureunit> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetUpdateCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Measureunit entity = list[i];
				SetUpdateValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			}
			return res;
		}
		
		#endregion
		#region Save
		public static bool SaveMeasureunit(Measureunit entity)
		{
			bool res = true;
			if (entity.Measureunitid == null )
			{
				res = InsertMeasureunit(entity);
			}
			else
			{
				res = UpdateMeasureunit(entity);
			}
			return res;
		}
		public static bool SaveMeasureunit(Measureunit entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			if (entity.Measureunitid == null )
			{
				res = InsertMeasureunit(entity, transactionObj, db);
			}
			else
			{
				res = UpdateMeasureunit(entity, transactionObj, db);
			}
			return res;
		}
		public static bool SaveListMeasureunit(List<Measureunit> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Measureunit entity = list[i];
					res = res & SaveMeasureunit(entity, transactionObj, db);
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool SaveListMeasureunit(List<Measureunit> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			for(int i=0; i<list.Count; i++)
			{
				Measureunit entity = list[i];
				res = res & SaveMeasureunit(entity, transactionObj, db);
			}
			return res;
		}
		#endregion
		#region Delete
		private static DbCommand deleteCommand = null;
		private static string deleteSQL = "DELETE FROM dbo.MeasureUnit WHERE MEASUREUNITID = @MEASUREUNITID";
		private static DbCommand GetDeleteCommand(SqlDatabase db)
		{
			if (deleteCommand == null)
			{
				deleteCommand = db.GetSqlStringCommand(deleteSQL);
				db.AddInParameter(deleteCommand, "@MEASUREUNITID", DbType.Int32);
			}
			return deleteCommand;
		}
		private static void SetDeleteValues(SqlDatabase db, DbCommand cmd, Measureunit entity)
		{
			db.SetParameterValue(cmd, "@MEASUREUNITID", entity.Measureunitid);
		}
		public static bool DeleteMeasureunit(Measureunit entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetDeleteCommand(db);
			SetDeleteValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool DeleteListMeasureunit(List<Measureunit> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetDeleteCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Measureunit entity = list[i];
					SetDeleteValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		
		#endregion
		#region Select
	
		public static List<Measureunit> LoadMeasureunits(string where, string orderby)
		{
			string selectSQL = "SELECT MEASUREUNITID,CATEGORYID,DESCRIPTION FROM dbo.MeasureUnit {0} {1}";
            List<Measureunit> res = new List<Measureunit>();
			string sql = String.Format(selectSQL, where, orderby);
			IDataReader reader = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				reader = db.ExecuteReader(CommandType.Text, sql);
				if (reader != null)
				{
					while (reader.Read())
					{
						Measureunit entity = new Measureunit();
						entity.Measureunitid = reader.IsDBNull(0) ? null : (int?)reader.GetInt32(0);
						entity.Categoryid = (Measureunit.MeasureCategory)reader.GetInt32(1);
						entity.Description = reader.IsDBNull(2) ? null : (string)reader.GetString(2);
						res.Add(entity);
					}
					reader.Close();
				}
			}
			catch (Exception ex)
			{
				if (reader != null && !reader.IsClosed)
				{
					reader.Close();
				}
				res = null;
				throw ex;
			}
			return res;
		}

        public static List<int> GetMeasureUnitIds(string criteria)
        {
            List<int> res = new List<int>();
            string selectSql = String.Format("SELECT MeasureUnitID FROM dbo.MeasureUnit {0}", criteria);
            IDataReader reader = null;
            try
            {
                SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
                reader = db.ExecuteReader(CommandType.Text, selectSql);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        int id = reader.GetInt32(0);
                        res.Add(id);
                    }
                    reader.Read();
                }
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                throw ex;
            }
            return res;
        }

		#endregion
	}
}
